<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page.sync="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @on-load="onLoad">
      <template slot="menuLeft">
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   v-if="permission.deviceinspectionstandard_delete"
                   @click="handleDelete">删 除
        </el-button>
      </template>
      <template slot="deviceForm">
        <div v-if="dialogType !== 'view'">
          <el-button
            size="small" 
            type="primary"
            @click="selectDevice">
            选择设备
          </el-button>
        </div>
      </template>
      <!-- <template slot="sbmc" slot-scope="scope">
        <div class="ope_row_div">
          <span v-if="scope.row['sblx'] === 1">特种设备-{{scope.row['sbmc']}}</span>
          <span v-else-if="scope.row['sblx'] === 2">生产设备-{{scope.row['sbmc']}}</span>
        </div>
      </template> -->
    </avue-crud>
    <!-- 弹窗 -->
    <el-dialog 
      title="选择设备"
      append-to-body
      :visible.sync="selectDeviceDialog"
      :close-on-click-modal="false" 
      width="20%"
      class="add_relation_dialog"
    >
    <el-tree
      :data="deviceList"
      show-checkbox
      node-key="id"
      :props="defaultProps"
      @check-change="selectTreeNode">
    </el-tree>
      <!-- 按钮 -->
      <div class="add_relation_footer"> 
        <el-button 
          type="primary"
          size="small"
          @click="confirmDevice()">
          确 定
        </el-button>
        <el-button 
          type="primary"
          size="small"
          @click="closeDeviceDialog()">
          关 闭
        </el-button>
      </div>
    </el-dialog>
  </basic-container>
</template>

<script>
  import {getList, getDeviceList,getDetail, add, update, remove} from "@/api/equipmentmanage/inspection/deviceinspectionstandard";
  import {mapGetters} from "vuex";

  export default {
    data() {
      return {
        form: {},
        query: {},
        loading: true,
        page: {
          pageSize: 30,
          currentPage: 1,
          total: 0
        },
        deviceList: [],
        selectTreeOption:{},
        defaultProps: {
          label: 'sbmc',
          value: 'id'
        },
        selectDeviceDialog:false,
        selectionList: [],
        option: {
          height:'auto',
          calcHeight: 30,
          tip: false,
          searchShow: true,
          searchMenuSpan: 6,
          border: true,
          dialogWidth: "60%",
          index: false,
          viewBtn: true,
          selection: true,
          dialogClickModal: false,
          column: [
            {
              label: "设备名称",
              prop: "sbmc",
              search:true,
              disabled:true,
              align: "center",
            },
            {
              label: "",
              labelWidth:20,
              formslot: true,
              rules: [{
                required: false,
                message: "请选择设备",
                trigger: "blur"
              }],
              prop: "device",
              hide: true,

            },
            {
              label: "项目名称",
              prop: "sbxmmc",
              rules: [{
                required: true,
                message: "请输入设备项目名称",
                trigger: "blur"
              }]
            },
            {
              label: "检查标准",
              prop: "jcbz",
              rules: [{
                required: false,
                message: "请输入检查标准",
                trigger: "blur"
              }]
            },
            {
              label: "单位",
              prop: "dw",
              rules: [{
                required: false,
                message: "请输入单位",
                trigger: "blur"
              }]
            },
            {
              label: "标准值下限",
              prop: "bzzxx",
              rules: [{
                required: false,
                message: "请输入标准值下限",
                trigger: "blur"
              }]
            },
            {
              label: "标准值上限",
              prop: "bzzsx",
              rules: [{
                required: false,
                message: "请输入标准值上限",
                trigger: "blur"
              }]
            },
            {
              label: "点检方法",
              prop: "djff",
              rules: [{
                required: false,
                message: "请输入点检方法",
                trigger: "blur"
              }]
            },
            {
              label: "巡检频次",
              prop: "xjpc",
              align: "center",
              search: false,
              type: "select",
              dicUrl: "/api/blade-system/dict-biz/dictionary?code=inspection_frequency",
              props: {
                label: "dictValue",
                value: "dictKey",
              }
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.deviceinspectionstandard_add, false),
          viewBtn: this.vaildData(this.permission.deviceinspectionstandard_view, false),
          delBtn: this.vaildData(this.permission.deviceinspectionstandard_delete, false),
          editBtn: this.vaildData(this.permission.deviceinspectionstandard_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      selectDevice(){
        this.deviceList=[]
        this.selectDeviceDialog=true;
        getDeviceList().then(res => {
          const data = res.data.data;
          data.forEach(ele => {
          this.deviceList.push(ele);
        });
          this.loading = false;
        });
      },
      confirmDevice(){
        this.selectDeviceDialog=false;
      },
      closeDeviceDialog(){
        this.selectDeviceDialog=false
      },
      selectTreeNode(data, checked) {
        if (checked) {
          if (data.children.length == 0) {
            this.selectTreeOption = data
          }
          console.log(this.selectTreeOption)
        }
      },
      rowSave(row, done, loading) {
        row["deviceInfoId"] = this.selectTreeOption.id;
        row["sblx"] = this.selectTreeOption.sblx;
        add(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          console.log(error);
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      onLoad(page, params = {}) {
        this.loading = true;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      }
    }
  };
</script>

<style>
.add_relation_footer {
  text-align: right;
  margin-top: 15px;
}
</style>
